<%@page contentType="text/html; charset=utf-8"%>
<%@ include file="../inc/TagLibs.jsp"%>
<c:set var="pageTitle" value="项目价格" />
<c:if test="${estType=='CLIENT'}">
	<c:set var="estTypeName" value="客户" />
</c:if>
<c:if test="${estType=='PROVIDER'}">
	<c:set var="estTypeName" value="供应商" />
</c:if>
<c:if test="${estType=='COOPERATOR'}">
	<c:set var="estTypeName" value="外协商" />
</c:if>
<%@ include file="../inc/Title.jsp"%>
<head>
<link rel="stylesheet" type="text/css" href="/css/base.css" />
<script src="/js/popup_ext.js"></script>
<script src="/js/jquery.js"></script>
<script language="javascript">
	function doSave(){
		document.getElementById("buttonDiv").style.display="none";
		document.getElementById("waitDiv").style.display="";
		frm.submit();
	}
	function doCancel(){
		parent.ClosePop();
	}
	function mtAddOnClick(){
		selectMakeType(this.getAttribute("index"));
	}
	function proAddOnClick(){
		selectProduct(this.getAttribute("index"));
	}
	function reqAddOnClick(){
	   selectMakeRequire(this.getAttribute("index"));
	}
	function unitAddOnClick(){
	   selectUnit(this.getAttribute("index"));
	}
	function doAddPrice(){
		var url="/base/MakeTypeListForMultiSelect.ihtm";
		ShowIframe("选择项目类型",url,360,500,"doAddPriceCallBack()");
	}
	function doAddPriceCallBack(){
		if(popRetVal!=null){
			for(i=0;i<popRetVal.length;i++){
				var newIndex=doItemAdd();
				document.getElementById("mt_id_"+newIndex).value=popRetVal[i][0];
				document.getElementById("mt_name_"+newIndex).value=popRetVal[i][1];
			}
		}
	}
	function elementOnChnage(){
		checkItemChange(this.getAttribute("index"));
	}
	function pnOnChange(){
		doProNameChange(this.getAttribute("index"));
	}
	function doItemAdd(defVals){
		var tbl=document.getElementById("itemRowTbl");
		var itemNum=document.getElementById("item_num").value-0;

		var tr=document.createElement("tr");
		tr.setAttribute("id","itemTr"+itemNum);
		
		var td;
		var input;
		var busiProd="${enterpriseInfo.busiProd}";
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="74px";
		input.setAttribute("index",itemNum);
		input.setAttribute("name","mt_name_"+itemNum);
		input.setAttribute("id","mt_name_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[0]);
		}
		input.setAttribute("readOnly",true);
		input.setAttribute("className","TextBox readonly");
		input.setAttribute("class","TextBox readonly");
		td.appendChild(input);
		
		input=document.createElement("input");
		input.setAttribute("type","hidden");
		input.setAttribute("name","mt_id_"+itemNum);
		input.setAttribute("id","mt_id_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[1]);
		}else{
			input.setAttribute("value","0");
		}
		td.appendChild(input);
		
		td.appendChild(document.createTextNode(" "));
		input=document.createElement("img");
		input.setAttribute("src","/images/icon_add.gif");
		input.setAttribute("index",itemNum);
		input.style.cursor="pointer";
		input.onclick=mtAddOnClick;
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="74px";
		input.setAttribute("maxLength","100");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","pro_name_"+itemNum);
		input.setAttribute("id","pro_name_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[3]);
		}
		if(busiProd=="SELECT"){
			input.setAttribute("className","TextBox readonly");
			input.setAttribute("class","TextBox readonly");
			input.setAttribute("readOnly",true);
		}else{
			input.setAttribute("className","TextBox");
			input.setAttribute("class","TextBox");
		}
		input.onchange=pnOnChange;
		td.appendChild(input);
		
		td.appendChild(document.createTextNode(" "));
		input=document.createElement("img");
		input.setAttribute("src","/images/icon_add.gif");
		input.setAttribute("index",itemNum);
		input.setAttribute("title","价格只与项目类型相关，材质可以直接录入，也可以选择库存材料");
		input.style.cursor="pointer";
		input.onclick=proAddOnClick;
		td.appendChild(input);
		
		input=document.createElement("span");
		input.setAttribute("id","proFlagSpan_"+itemNum);
		input.setAttribute("className","MustInput");
		input.setAttribute("class","MustInput");
		input.appendChild(document.createTextNode("*"));
		if(defVals==null || defVals[2]=="0"){
			input.style.display="none";
		}
		td.appendChild(input);
		
		input=document.createElement("input");
		input.setAttribute("type","hidden");
		input.setAttribute("name","pro_id_"+itemNum);
		input.setAttribute("id","pro_id_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[2]);
		}else{
			input.setAttribute("value","0");
		}
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="36px";
		input.setAttribute("maxLength","10");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","length_"+itemNum);
		input.setAttribute("id","length_"+itemNum);
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		if(defVals!=null){
			input.setAttribute("value",defVals[8]);
		}
		input.onchange=elementOnChnage;
		td.appendChild(input);
		td.appendChild(document.createTextNode(" x "));
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="36px";
		input.setAttribute("maxLength","10");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","width_"+itemNum);
		input.setAttribute("id","width_"+itemNum);
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		if(defVals!=null){
			input.setAttribute("value",defVals[9]);
		}
		input.onchange=elementOnChnage;
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="88px";
		input.setAttribute("maxLength","50");
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","specification_"+itemNum);
		input.setAttribute("id","specification_"+itemNum);
		if(defVals!=null){
			input.value=defVals[10];
		}
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="40px";
		input.setAttribute("maxLength","10");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","unit_"+itemNum);
		input.setAttribute("id","unit_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[5]);
		}
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		td.appendChild(input);
		
		td.appendChild(document.createTextNode(" "));
		input=document.createElement("img");
		input.setAttribute("src","/images/icon_add.gif");
		input.setAttribute("index",itemNum);
		input.style.cursor="pointer";
		input.onclick=unitAddOnClick;
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.cssText="text-align:right";//chrome中这句需要放在设置input.style.width的前面,否则width无效
		input.style.width="50px";
		input.setAttribute("maxLength","10");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","price_"+itemNum);
		input.setAttribute("id","price_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[6]);
		}else{
			input.setAttribute("value","");
		}
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("textarea");
		input.style.width="100px";
		input.style.height="60px";
		input.setAttribute("index",itemNum);
		input.setAttribute("name","mk_req_"+itemNum);
		input.setAttribute("id","mk_req_"+itemNum);
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		if(defVals!=null){
			input.setAttribute("value",defVals[4]);
		}
		td.appendChild(input);
		
		td.appendChild(document.createTextNode(" "));
		input=document.createElement("img");
		input.setAttribute("src","/images/icon_add.gif");
		input.setAttribute("index",itemNum);
		input.style.cursor="pointer";
		input.onclick=reqAddOnClick;
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		input=document.createElement("input");
		input.setAttribute("type","text");
		input.style.width="88px";
		input.setAttribute("maxLength","100");
		input.setAttribute("index",itemNum);
		input.setAttribute("name","mark_"+itemNum);
		input.setAttribute("id","mark_"+itemNum);
		if(defVals!=null){
			input.setAttribute("value",defVals[7]);
		}
		input.setAttribute("className","TextBox");
		input.setAttribute("class","TextBox");
		td.appendChild(input);
		tr.appendChild(td);
		
		td=document.createElement("td");
		var delLink=document.createElement("a");
		delLink.setAttribute("href","javascript:doItemDelete("+itemNum+");");
		delLink.appendChild(document.createTextNode("删除"));
		td.appendChild(delLink);
		td.appendChild(document.createTextNode(" "));
		var copyLink=document.createElement("a");
		copyLink.setAttribute("href","javascript:doItemCopy("+itemNum+");");
		copyLink.appendChild(document.createTextNode("复制"));
		td.appendChild(copyLink);
		
		input=document.createElement("input");
		input.setAttribute("type","hidden");
		input.setAttribute("name","status_"+itemNum);
		input.setAttribute("id","status_"+itemNum);
		input.setAttribute("value","NEW");
		td.appendChild(input);
		tr.appendChild(td);

		tbl.appendChild(tr);
		document.getElementById("item_num").value=itemNum+1;
		return itemNum;//当前行的index
	}
	function doItemDelete(index){
		ShowConfirm('确认框','确定删除吗？',340,80,"doItemDelAct("+index+")");
	}
	function doItemDelAct(index){
		if(popRetVal!=null){
			document.getElementById("itemTr"+index).style.display="none";
			document.getElementById("status_"+index).value="DELETE";
		}
	}
	function doItemCopy(index){
	   var defVals=new Array();
	   defVals[0]=document.getElementById("mt_name_"+index).value
	   defVals[1]=document.getElementById("mt_id_"+index).value;
	   defVals[2]=document.getElementById("pro_id_"+index).value;
	   defVals[3]=document.getElementById("pro_name_"+index).value;
	   defVals[4]=document.getElementById("mk_req_"+index).value;
	   defVals[5]=document.getElementById("unit_"+index).value;
	   defVals[6]=document.getElementById("price_"+index).value;
	   defVals[7]=document.getElementById("mark_"+index).value;
	   defVals[8]=document.getElementById("length_"+index).value;
	   defVals[9]=document.getElementById("width_"+index).value;
	   defVals[10]=document.getElementById("specification_"+index).value;
	   doItemAdd(defVals);
	}
	function selectProduct(index){
		var url="/base/ProductMainForSelect.ihtm";
		ShowIframe("选择材料",url,900,500,"selectProductCallBack("+index+")");
	}
	function selectProductCallBack(index){
		if(popRetVal!=null){
			document.getElementById("pro_id_"+index).value=popRetVal[0];
			document.getElementById("pro_name_"+index).value=popRetVal[2];
			document.getElementById("unit_"+index).value=popRetVal[3];
			document.getElementById("proFlagSpan_"+index).style.display="";
		}
	}
	function selectMakeRequire(index){
		var url="/base/MakeRequireListForSelect.ihtm";
		ShowIframe("选择制作要求",url,900,500,"selectMakeRequireCallBack("+index+")");
	}
	function selectMakeRequireCallBack(index){
		if(popRetVal!=null){
			var theValue="";
			for(i=0;i<popRetVal.length;i++){
				if(theValue!=null && theValue!=""){
					theValue=theValue+";";
				}
				theValue=theValue+popRetVal[i][0];
			}
			document.getElementById("mk_req_"+index).value=theValue;
		}
	}
	function selectUnit(index){
		var url="/base/UnitListForSelect.ihtm";
		ShowIframe("选择单位",url,300,400,"selectUnitCallBack("+index+")");
	}
	function selectUnitCallBack(index){
		if(popRetVal!=null){
			document.getElementById("unit_"+index).value=popRetVal[1];
		}
	}
	function selectMakeType(index){
		var url="/base/MakeTypeListForSelect.ihtm";
		ShowIframe("选择项目类型",url,310,500,"selectMakeTypeCallBack("+index+")");
	}
	function selectMakeTypeCallBack(index){
		if(popRetVal!=null){
			document.getElementById("mt_id_"+index).value=popRetVal[0];
			document.getElementById("mt_name_"+index).value=popRetVal[1];
		}
	}
	
	function doSelectEstPrice(priceType){
		if(priceType=="EST"){
			var url="/base/EstablishmentListForSelect.ihtm?type=${estType}";
			ShowIframe("选择${estTypeName}",url,900,500,"doSelectEstPriceEstExt()");
		}else{
			var url="/base/MakeTypeListForMultiSelect.ihtm";
			ShowIframe("选择项目类型",url,360,500,"doSelectEstPriceStaExt()");
		}
	}
	function doSelectEstPriceEstExt(){
		if(popRetVal!=null){
			document.getElementById("LoadProcess").style.top=getTopPos(document.getElementById("addLink"))+50+"px";
			document.getElementById("LoadProcess").style.left=getleftPos(document.getElementById("addLink"))+100+"px";
			$("#LoadProcess").show();
			
			var estId=popRetVal[0];
			var estType="${estType}";
			$.post("/base/GetEstPricList.ihtm",{est_type:estType,price_type:"EST",est_id:estId},function(data,status){
				parsePriceData(data,status);
			});
		}
	}
	
	function doSelectEstPriceStaExt(){
		if(popRetVal!=null){
			var str="";
			for(i=0;i<popRetVal.length;i++){
				if(str!=""){
					str=str+";";
				}
				str=str+popRetVal[i][0];
			}
			if(str!=""){
				document.getElementById("LoadProcess").style.top=getTopPos(document.getElementById("addLink"))+50+"px";
				document.getElementById("LoadProcess").style.left=getleftPos(document.getElementById("addLink"))+100+"px";
				$("#LoadProcess").show();
				
				var estType="${estType}";
				$.post("/base/GetEstPricList.ihtm",{est_type:estType,price_type:"STA",mt_id:str},function(data,status){
					parsePriceData(data,status);
				});
			}
		}
	}
	function doSelectClientSta(){
		var url="/base/MakeTypeListForMultiSelect.ihtm";
		ShowIframe("选择项目类型",url,360,500,"doSelectClientStaExt()");
	}
	function doSelectClientStaExt(){
		if(popRetVal!=null){
			var str="";
			for(i=0;i<popRetVal.length;i++){
				if(str!=""){
					str=str+";";
				}
				str=str+popRetVal[i][0];
			}
			if(str!=""){
				document.getElementById("LoadProcess").style.top=getTopPos(document.getElementById("addLink"))+50+"px";
				document.getElementById("LoadProcess").style.left=getleftPos(document.getElementById("addLink"))+100+"px";
				$("#LoadProcess").show();
				$.post("/base/GetEstPricList.ihtm",{est_type:"CLIENT",price_type:"STA",mt_id:str},function(data,status){
					parsePriceData(data,status);
				});
			}
		}
	}
	function parsePriceData(data,status){
		if(status=="success"){
			var obj=$.parseJSON(data);
			if(obj.status=="ERROR"){
				$("#LoadProcess").hide();
				ShowAlert('提示框',obj.errMsg,200,100);
			}else{
				$.each(obj.items,function(index,item){
					var newIndex=doItemAdd();
					$("#mt_id_"+newIndex).val(item.mtId);
					$("#mt_name_"+newIndex).val(item.mtName);
					$("#pro_id_"+newIndex).val(item.proId);
					$("#pro_name_"+newIndex).val(item.proName);
					$("#mk_req_"+newIndex).val(item.requ);
					$("#unit_"+newIndex).val(item.unit);
					$("#price_"+newIndex).val(item.pric);
					$("#mark_"+newIndex).val(item.mark);
					$("#length_"+newIndex).val(item.leng);
					$("#width_"+newIndex).val(item.widt);
					$("#specification_"+newIndex).val(item.spec);
					
					if(item.proId!='0'){
						document.getElementById("proFlagSpan_"+newIndex).style.display="";
					}
				});
				$("#LoadProcess").hide();
			}
		}else{
			$("#LoadProcess").hide();
			ShowAlert('提示框','系统错误：'+status,200,100);
		}
	}
	function checkItemChange(index){
		var lengthStr=document.getElementById("length_"+index).value;
		var widthStr=document.getElementById("width_"+index).value;
		if(lengthStr!="" && widthStr!="" && !isNaN(lengthStr) && !isNaN(widthStr)){
			document.getElementById("specification_"+index).value=lengthStr+"m x "+widthStr+"m高";
		}
	}
	function doInit(){
		showFormErrorMsg();
	}
	function doProNameChange(index){
		document.getElementById("pro_id_"+index).value="0";
		document.getElementById("proFlagSpan_"+index).style.display="none";
	}
</script>
</head>
<body oncontextmenu="return false" onLoad="doInit();"  class="sheet modal-dialog" style=" padding-top:10px;">
<div class="sheet-main" id="bodyDiv" style="width:980px;">
<form name="frm" method="post">
  <div class="sheet-list" style="width:100%">
    <div class="modal-main search-business">
    	<div style="width:100%;">
			<c:if test="${priceType=='STA'}"><c:set var="pageTopTitle" value="标准价格" /></c:if><c:if test="${priceType=='EST'}"><c:set var="pageTopTitle" value="${est.estName}" /></c:if>
			<%@ include file="../inc/PageTop.jsp"%> 
			<div class="table-list">
				<input type="hidden" id="item_num" name="item_num" value="${fn:length(command.items)}">
				<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" id="itemTbl">
					<tbody id="itemRowTbl">
					<tr>
						<th width="12%">项目类型</th>
						<th width="12%">材质</th>
						<th width="12%">宽x高</th>
						<th width="11%">规格</th>
						<th width="9%">单位</th>
						<th width="8%">单价(元)</th>
						<th width="17%">制作要求</th>
						<th width="11%">备注</th>
						<th width="8%">操作</th>
					</tr>
					<c:forEach var="item" items="${command.items}" varStatus="i">
					<tr id="itemTr${i.index}">
						<td>
							<input type="text" class="TextBox readonly" style="width:74px" id="mt_name_${i.index}" name="mt_name_${i.index}" value="<c:out value="${item.mtName}" />" readonly="true">
							<input type="hidden" id="mt_id_${i.index}" name="mt_id_${i.index}" value="${item.estPrice.makeTypeId}">
							<img src="/images/icon_add.gif" style="cursor:pointer;" onClick="selectMakeType(${i.index});">
						</td>
						<td>
							<input type="text" <c:if test="${enterpriseInfo.busiProd=='SELECT'}">class="TextBox readonly" readonly="readonly"</c:if><c:if test="${enterpriseInfo.busiProd!='SELECT'}">class="TextBox"</c:if> style="width:74px" maxlength="100" id="pro_name_${i.index}" name="pro_name_${i.index}" value="<c:out value="${item.estPrice.proName}" />" onChange="doProNameChange(${i.index});">
							<img src="/images/icon_add.gif" style="cursor:pointer;" onClick="selectProduct(${i.index});" title="价格只与项目类型相关，材质可以直接录入，也可以选择库存材料"><span class="MustInput" id="proFlagSpan_${i.index}" <c:if test="${item.estPrice.proId==0}">style="display:none"</c:if>>*</span>
							<input type="hidden" id="pro_id_${i.index}" name="pro_id_${i.index}" value="${item.estPrice.proId}" >
						</td>
						<td >
							<input type="text" style="width:36px" maxlength="10" id="length_${i.index}" name="length_${i.index}" value="${item.estPrice.length}" onChange="checkItemChange(${i.index});">
							x
							<input type="text" style="width:36px" maxlength="10" id="width_${i.index}" name="width_${i.index}" value="${item.estPrice.width}" onChange="checkItemChange(${i.index});">
						</td>
						<td >
							<input type="text" style="width:88px" maxlength="50" name="specification_${i.index}" id="specification_${i.index}" value="<c:out value="${item.estPrice.specification}" />">
						</td>
						<td>
							<input type="text" class="TextBox" maxlength="10" style="width:40px" id="unit_${i.index}" name="unit_${i.index}" value="${item.estPrice.unit}">
							<img src="/images/icon_add.gif" style="cursor:pointer;" onClick="selectUnit(${i.index});">
						</td>
						<td>
							<input type="text" class="TextBox" style="width:50px;text-align:right;" maxlength="10" id="price_${i.index}" name="price_${i.index}" value="${item.priceStr}">
						</td>
						<td>
							<textarea style="width:100px;height:60px" id="mk_req_${i.index}" name="mk_req_${i.index}"><c:out value="${item.estPrice.makeRequire}" /></textarea>
							<img src="/images/icon_add.gif" style="cursor:pointer;" onClick="selectMakeRequire(${i.index});">
						</td>
						<td>
							<input type="text" class="TextBox" style="width:88px" maxlength="100" id="mark_${i.index}" name="mark_${i.index}" value="${item.estPrice.mark}">
						</td>
						<td>
							<a href="javascript:doItemDelete(${i.index});">删除</a>&nbsp;
							<a href="javascript:doItemCopy(${i.index});">复制</a>
							<input type="hidden" id="status_${i.index}" name="status_${i.index}" value="">
						</td>
					</tr>
					</c:forEach>
					</tbody>
				</table>
				<div class="table-bot">
					<a href="javascript:doAddPrice();" id="addLink">增加项目价格</a>
					<c:if test="${priceType=='EST'}">
						&nbsp;&nbsp;
						<a href="javascript:doSelectEstPrice('STA');">复制标准价格</a>
						&nbsp;&nbsp;
						<a href="javascript:doSelectEstPrice('EST');">复制其他${estTypeName}价格</a>
					</c:if>
					<c:if test="${priceType=='STA' && estType=='COOPERATOR'}">
						&nbsp;&nbsp;
						<a href="javascript:doSelectClientSta();">复制客户标准价格</a>
					</c:if>
				</div>					      
				
				<spring:bind path="command.items">
					<script language="javascript">
		          	setFormErrorMsg('<c:out value="${status.errorMessage}" />',1);
					</script>
				</spring:bind>
			</div>
	     </div>
	   </div>
    </div>
	<div class="modal-buttom" id="buttonDiv">
		<input type="button" class="buttom-a" value=" 保 存 " onClick="doSave();">
		<input type="button" class="buttom-a" value=" 取 消 " onClick="doCancel();">
	</div>
	<div class="modal-buttom" id="waitDiv" style="display:none">
		<span style="color:#ffffff">请稍候...</span>
	</div>
</form>
</div>
<%@ include file="../inc/LoadingDiv.jsp"%>
</body>
</html>